/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.oszc.bbhmlibrary.utils;

import ohos.agp.utils.Matrix;
import ohos.media.image.ImagePacker;
import ohos.media.image.PixelMap;

import java.io.File;
import java.io.FileOutputStream;

public class CommonUtils {
    private final static String TAG = CommonUtils.class.getSimpleName();


    public static void printMatrix(Matrix matrix) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < matrix.getData().length; i++) {

            sb.append(matrix.getData()[i]);
            sb.append(",");

            if((i>0 && (i+1)%3==0) ) {
                sb.deleteCharAt(sb.length()-1);
                sb.delete(0,sb.length());
            }
        }
    }
    //
    public static void writePixelMapToCacheForTest(PixelMap pixelmap,String path,String prefix,boolean override) {
        String displayName = "image.jpg";
        // 拷贝至应用根目录
        try {
            ImagePacker imagePacker = ImagePacker.create();
            FileOutputStream outputStream;
            String filename =path + prefix + displayName;
            File file = new File(filename);
            if(file.exists()&& !override){
                return;
            }

            outputStream = new FileOutputStream(filename);
            ImagePacker.PackingOptions packingOptions = new ImagePacker.PackingOptions();
            imagePacker.initializePacking(outputStream, packingOptions);
            imagePacker.addImage(pixelmap);
            imagePacker.finalizePacking();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
